<script setup>
import { computed, onMounted, reactive, ref, watchEffect } from 'vue';
const formLabelWidth = '150px'
import { ElMessage } from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { Decrypt, Encrypt } from '../../untils/passjs/security';
import { getFeedbackDetail, feedbackSubmitjiedian, AddFeedBack, getRoledata, editFeedBack, getRoledataList } from '../../api/api';
import { template } from 'lodash';
let formRef = ref(null)
let props = defineProps({
    dialogFormVisibleDetail: Boolean,
    formlist: Object,
    changeType: String,
})
let form = ref({})
const datasel = ref([]);
const states = ref([]);
const countryStates = ref({});
let activities = ref([])
// let iscancelbut = ref(true)
// let issubbut = ref(true)
let myUserId = ref(null)
const shouldShowBackButton = computed(() => {
    return (
        props.formlist.currentUserId == myUserId.value &&
        (props.formlist.progressStatus == 1 || props.formlist.progressStatus == 2 || props.formlist.progressStatus == 3)
    );
});
let isbut = ref(null)
watchEffect(async () => {
    if (props.dialogFormVisibleDetail) {
        myUserId.value = JSON.parse(localStorage.getItem('userdetail'))?.user?.userId
        if (props.formlist.userId == myUserId.value) {
            isbut.value = 1
        } else {
            if (props.formlist.currentUserId == myUserId.value) {
                isbut.value = 2
            } else {
                isbut.value = null
            }
        }
        try {
            let res = await getFeedbackDetail(props.formlist.feedbackId)
            if (res.code == 200) {
                activities.value = res.data?.progress
                form.value = res.data
            }
        } catch (error) {
            console.log(error);
        }
    }
})
let emits = defineEmits(["closeDetail", 'update:formlist'])
function closedialog(formEl) {
    if (!formEl) return
    emits("closeDetail")
}
function colseclose(formRef) {
    emits("closeDetail")
    buttontype.value = 'Submission type'
}

let buttontype = ref('Submission type')
let buttonVis = ref(false)
let nodenum = ref(null)
async function submitfun(val) {
    buttontype.value = val
    if (isbut.value == 1) {
        if (val == 'submit') {
            nodenum.value = 6
        } else if (val == 'return') {
            nodenum.value = 2
        } else if (val == 'nullify') {
            nodenum.value = 0
        }
        console.log(props.formlist.feedbackId, nodenum.value);
        let requestData = {
            documentId: props.formlist.feedbackId,
            nodeStatus: nodenum.value,
            leaveWord: ""
        }
        try {
            let res = await feedbackSubmitjiedian(requestData)
            if (res.code == 200) {
                ElMessage.success(res.msg)
                colseclose()
            }
        } catch (error) {
            console.log(error);

        }
    } else if (isbut.value == 2) {
        dialogVisibleleaveWord.value = true
        if (val == 'reject') {
            nodenum.value = 7
        } else if (val == 'processed') {
            nodenum.value = 4
        } else if (val == 'incomplete') {
            nodenum.value = 5
        } else if (val == 'submit upwards') {
            nodenum.value = 6
        }
    }
}
let dialogVisibleleaveWord = ref(false)
let leavewordstr = ref('')
async function currentSurefun() {
    if (leavewordstr.value == '') {
        ElMessage.warning('Please enter the processing message')
        return;
    }
    let requestData = {
        documentId: props.formlist.feedbackId,
        nodeStatus: nodenum.value,
        leaveWord: leavewordstr.value
    }
    try {
        let res = await feedbackSubmitjiedian(requestData)
        if (res.code == 200) {
            ElMessage.success(res.msg)
            colseclose()
            dialogVisibleleaveWord.value = false
        }
    } catch (error) {
        console.log(error);
    }

}
function switchnodeStatus(nodeStatus) {
    switch (nodeStatus) {
        case 0:
            return 'end';
            break;
        case 1:
            return 'processing';
            break;
        case 2:
            return 'return';
            break;
        case 3:
            return 'not sent';
            break;
        case 4:
            return 'processed completed';
            break;
        case 5:
            return 'processed but not completed';
            break;
        case 6:
            return 'unable to process upward submission';
            break;
        case 7:
            return 'reject';
            break;
        case 8:
            return 'untreated';
            break;
    }

}
const hasPermission = (permission) => {
    const permissions = JSON.parse(localStorage.getItem("userdetail"))?.auths || [];
    if (permissions[0] === '*:*:*') {
        return true;
    }
    return permissions.includes(permission);
};
</script>
<template>
    <div class="editclass">
        <el-dialog :model-value="dialogFormVisibleDetail" class="eldialogclass" style="padding: 30px;width: 800px;"
            title="detail" @close="colseclose">
            <div class="cont">
                <div class="topcalss" style="display: flex;justify-content: space-between;">
                    <div class="top" :title="form.feedbackTitle">
                        <span style="font-size: 20px;">feedbackTitle:</span>
                        {{
                            form.feedbackTitle && form.feedbackTitle.length > 10
                                ? form.feedbackTitle.slice(0, 10) + '...'
                                : form.feedbackTitle
                        }}
                    </div>
                    <div class="dialog-footer" style="margin: 20px 50px 0 0;display: flex;">
                        <template v-if="isbut == 1 && hasPermission('feedback:progress:submit')">
                            <el-dropdown style="cursor: pointer;;border: 0;margin: 0 5px;">
                                <el-button type="primary">{{ buttontype }}</el-button>
                                <template #dropdown>
                                    <el-dropdown-menu style="width: 80px;">
                                        <el-dropdown-item @click="submitfun('submit')">submit</el-dropdown-item>
                                        <el-dropdown-item @click="submitfun('return')">return</el-dropdown-item>
                                        <el-dropdown-item @click="submitfun('nullify')">nullify</el-dropdown-item>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </template>
                        <template v-else-if="isbut == 2 && hasPermission('feedback:progress:submit')">
                            <el-dropdown style="cursor: pointer;background-color: #fff;border: 0;margin: 0 5px;">
                                <el-button type="primary">{{ buttontype }}</el-button>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item @click="submitfun('reject')">reject</el-dropdown-item>
                                        <el-dropdown-item @click="submitfun('processed')">processed</el-dropdown-item>
                                        <el-dropdown-item @click="submitfun('incomplete')">incomplete</el-dropdown-item>
                                        <el-dropdown-item @click="submitfun('submit upwards')">submit
                                            upwards</el-dropdown-item>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </template>
                        <template v-else>

                        </template>

                    </div>
                </div>
                <div class="feedbackperson"><span style="font-size: 16px;">Processing person:</span>{{
                    form.nickName }}</div>
                <div class="feedbackcontent">feedbackContent:{{ form.feedbackContent }}</div>
                <div class="nodeclass">
                    <el-timeline class="custom-timeline">
                        <el-timeline-item v-for="(item, index) in activities" :key="index" color="#6ad1b2" :hollow="true"
                            :timestamp="item.createTime">
                            {{ item.progressNickname ? item.progressNickname : '' }} --- {{
                                switchnodeStatus(item.nodeStatus) }}
                            <br /><span style="font-size: 12px;color: gray;display: flex;align-items: center;"><el-icon size="12" style="margin-right: 6px"><Edit /></el-icon>{{ item.leaveWord }}</span>
                        </el-timeline-item>
                    </el-timeline>
                </div>
            </div>
        </el-dialog>
        <el-dialog v-model="dialogVisibleleaveWord" title="Handling prompts" width="500">
            <template #footer>
                <el-input type="textarea" :autosize="{ minRows: 2 }" placeholder="Please enter the processing message"
                    v-model="leavewordstr"></el-input>
                <div class="dialog-footer" style="margin-top: 20px;">
                    <el-button @click="() => { dialogVisibleleaveWord = false; leavewordstr = '' }">cancel</el-button>
                    <el-button type="primary" @click="currentSurefun()">
                        sure
                    </el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>
<style lang="scss" scoped>
.cont {
    .top {
        font-size: 30px;
    }

    .feedbackperson {
        font-size: 12px;
        margin: 5px 0;
    }

    .feedbackcontent {
        width: 90%;
        padding: 25px 20px;
        background-color: #f5f5f5;
        font-size: 20px;
    }

    .nodeclass {
        // width: 200px;
        // background-color: azure;
        margin-top: 10px;
        padding-top: 20px;

        /* 使用 :deep() 穿透到 el-timeline-item，修改其 margin-bottom */
        // .custom-timeline {
        //     :deep(.el-timeline-item) {
        //         margin-bottom: -15px;
        //         /* 默认可能是 16px ~ 20px，你调小成 8px / 12px / 16px 等 */
        //         // 如果想同时调整 padding，也可以加：
        //         // padding-bottom: 8px;
        //     }

        //     /* 可选：如果最后一个节点不需要下边距，可以进一步优化 */
        //     :deep(.el-timeline-item:last-child) {
        //         margin-bottom: 0; // 最后一个节点下面不需要间距
        //     }
        // }
    }
}

/* 响应式设计 - 小屏幕时变为单列 */
@media (max-width: 768px) {
    .cont {
        width: 400px;
    }

    :deep(.el-dialog) {
        width: 500px !important;
    }
}

</style>